import redis
import  time
class icanSeqTaskSchedule():

    def __init__(self, host='localhost',port=6379):
        pool = redis.ConnectionPool(host=host, port=port, db=0)
        self.r = redis.StrictRedis(connection_pool=pool)


    def  run(self):

        self.r.delete('RunningQueue')
        self.r.delete('PendingQueue')
        while True:
            time.sleep(0.5)
            if self.r.llen('RunningQueue') >= 1:
                continue

            command = self.r.brpop('PendingQueue',timeout=1)

            if command is not None:
                print command
                p = self.r.publish('SubcribeQueue',command)
                self.r.lpush('RunningQueue',command[1])
                print '%d subscribers: command %s '%(p,command)


if __name__ == '__main__':

    inst = icanSeqTaskSchedule()
    inst.run()




